<template>
	<view>
		<u-navbar>
			<view class="slot-wrap">
				<u-search placeholder="输入中文/拼音/首字母" v-model="keyword" @focus='focusFn' @change="changeFn"></u-search>
			</view>
		</u-navbar>
		<view class="container" v-if="!isFocus">
			<view class="citynow">
				<view class="lable">
					当前/历史
				</view>
				<view class="city-ul">
					<view class="city-li" @click="choseAddress(city)">
						<u-icon name="map-fill" color="#2979ff" size="28"></u-icon>{{city}}
					</view>
					<view class="city-li" v-for="(his,hisx) in cityHistory" :key='hisx' @click="choseAddress(his)">
						{{his}}
					</view>
				</view>
			</view>
			<view class="cityIndex">
				<u-index-list :scrollTop="scrollTop" :index-list="indexList">
					<view v-for="(item, index) in cityList" :key='index'>
						<u-index-anchor :index="item.letter" />
						<view class="list-cell u-border-bottom" v-for="(city, cityx) in item.data" :key="cityx"
							@click="choseAddress(city)">
							{{city}}
						</view>
					</view>
				</u-index-list>
			</view>
		</view>
		<view class="cityBox" v-else>
			<view class="box-ul">
				<view class="box-li" v-for="(res,resx) in searchList" :key='index' @click="choseAddress(res.city_name)">
					{{res.airport_name}}
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	import myRequest from '@/api/index.js'
	export default {
		data() {
			return {
				keyword: '',
				title: '选择出发站',
				scrollTop: 0,
				list: [],
				indexList: ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S",
					"T", "W", "X", "Y", "Z"
				],
				cityList: [],
				cityHistory: [],
				city: '',
				start: 0,
				isFocus: false,
				listCity: [], // 初始城市列表
				searchList:[]
			}
		},
		onLoad(option) {
			this.start = option.start
			let that = this
			uni.getLocation({
				type: 'gcj02',
				geocode: true,
				success(res) {
					that.city = res.address.city
				}
			})
			this.getAirportList()
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		onShow() {
			// 获取历史
			let cityHistory = uni.getStorageSync('cityHistory')
			if (cityHistory) {
				let lits = [...new Set(cityHistory)]
				this.cityHistory = lits
			} else {
				this.cityHistory = []
			}
		},
		methods: {
			changeFn() {
				if(this.keyword) {
					let list = []
					this.listCity.forEach(item => {
						if(item.city_name.indexOf(this.keyword) != -1) {
							list.push(item)
						}
						if(item.city_name_en.indexOf(this.keyword) != -1) {
							list.push(item)
						}
						if(item.city_code.indexOf(this.keyword) != -1) {
							list.push(item)
						}
					})
					this.searchList = list
					console.log(this.searchList)
				}else {
					this.searchList = []
				}
			},
			focusFn() {
				this.isFocus = true
			},
			choseAddress(city) {
				let that = this
				if (this.cityHistory.length < 7) {
					this.cityHistory.push(city)
				} else {
					this.cityHistory.splice(0, 1)
					this.cityHistory.push(city)
				}
				// 选择地址跳转并保存
				uni.setStorage({
					key: 'cityHistory',
					data: this.cityHistory.reverse(),
					success() {
						if (that.start == 0) {
							that.$store.commit('changeStart', city)
						}
						if (that.start == 1) {
							that.$store.commit('changeEnd', city)
						}
						uni.navigateTo({
							url: '../planeTicket'
						})
					}
				})
			},
			// 获取机场
			async getAirportList() {
				let res = await myRequest.getAirport()
				if (res.status) {
					this.listCity = res.data
					// console.log(res.data)
					// 排序处理
					let sortData = this.sortName(res.data, 'city_name')
					let cityList = []
					this.indexList.forEach(item => {
						
						let obj = {
							letter: '',
							data: []
						}
						sortData.forEach(city => {
							let str = city.city_name_en.substr(0, 1)
							if (str.toUpperCase() == item) {
								obj.letter = item
								obj.data.push(city.city_name)
								obj.data = [...new Set(obj.data)]
							}
						})
						cityList.push(obj)
					})
					this.cityList = cityList.filter(item => {
						return item.letter != ''
					})
				}
			},
			// 排序
			sortName(arr, eachName) {
				arr.forEach(function(item) {
					let temp = item[eachName];
					item.sortName = temp;
				});
				let resultArray = arr.sort(
					function compareFunction(param1, param2) {
						return param1.sortName.localeCompare(param2.sortName, "zh");
					}
				);
				return resultArray;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.slot-wrap {
		width: 624rpx;
		height: 64rpx;
		background: #F4F4F4;
		border-radius: 40rpx;
		display: flex;
		align-items: center;
	}

	.container {

		.citynow {
			padding: 24rpx 28rpx 0;

			.lable {
				font-size: 32rpx;
				font-family: Segoe UI;
				font-weight: bold;
				color: #333333;
			}

			.city-ul {
				display: flex;
				align-items: center;
				flex-wrap: wrap;
				margin-top: 18rpx;

				.city-li {
					width: 152rpx;
					height: 66rpx;
					background: #F8F8F8;
					border-radius: 4rpx;
					text-align: center;
					line-height: 66rpx;
					margin-bottom: 16rpx;
					margin-right: 24rpx;

					image {
						width: 28rpx;
						height: 28rpx;
						margin-right: 8rpx;
					}

					&:nth-child(4n) {
						margin-right: 0rpx;
					}
				}
			}
		}
	}
		
	.cityBox {
		width: 100%;
		height: auto;
		background-color: #FFFFFF;
		.box-ul {
			padding: 0 20rpx;
			.box-li {
				width: 100%;
				height: 88rpx;
				line-height: 88rpx;
			}
		}
	}

	.cityIndex {
		width: 100%;
		height: auto;
	}

	.list-cell {
		display: flex;
		box-sizing: border-box;
		width: 100%;
		padding: 20rpx 24rpx;
		overflow: hidden;
		color: #323233;
		font-size: 14px;
		line-height: 24px;
		background-color: #fff;
	}
</style>
